
package com.mephi.gui;


import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;


/**
 * 
 * This class extends javax.swing.JButton 
 * @since 2011-04-09
 */
public class TranslucentButton extends JButton 
{
	/**
	 * 
	 * It creates a new instance of TranslucentButton 
	 * @param label the label to set
	 */
    public TranslucentButton(String label)
    {
        super(label);
        setOpaque(false);
    }
    
    /**
	 * 
	 * @param g the g to set 
	 */
    public void paint(Graphics g) 
    {
        // Create an image for the button graphics if necessary
        if (buttonImage == null || buttonImage.getWidth() != getWidth() ||
                buttonImage.getHeight() != getHeight()) 
        {
            buttonImage = getGraphicsConfiguration().
                    createCompatibleImage(getWidth(), getHeight());
        }
        
        Graphics gButton = buttonImage.getGraphics();
        gButton.setClip(g.getClip());
        
        //  Have the superclass render the button for us
        super.paint(gButton);
        
        // Make the graphics object sent to this paint() method translucent
		Graphics2D g2d  = (Graphics2D)g;
		AlphaComposite newComposite = 
		    AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
		g2d.setComposite(newComposite);
        
        // Copy the button's image to the destination graphics, translucently
        g2d.drawImage(buttonImage, 0, 0, null);
    }
    
    private static final long serialVersionUID = 5377466761326849093L;
    private BufferedImage buttonImage = null;
}
